Notes

require vs library library provides an error message require returns

#load libraries
library(raster)
## Loading required package: sp
library(rgdal)
## rgdal: version: 1.1-10, (SVN revision 622)
##  Geospatial Data Abstraction Library extensions to R successfully loaded
##  Loaded GDAL runtime: GDAL 1.11.4, released 2016/01/25
##  Path to GDAL shared files: /Library/Frameworks/R.framework/Versions/3.3/Resources/library/rgdal/gdal
##  Loaded PROJ.4 runtime: Rel. 4.9.1, 04 March 2015, [PJ_VERSION: 491]
##  Path to PROJ.4 shared files: /Library/Frameworks/R.framework/Versions/3.3/Resources/library/rgdal/proj
##  Linking to sp version: 1.2-3

Import Canopy Height Model

chm <- raster("../NEONdata/D17-California/TEAK/2013/lidar/TEAK_lidarCHM.tif")
plot(chm,
    main = "this plots using the raster package")

image(chm,
      main = "these are just pixels and will stretch the space")

Deal with 0 values

hist(chm)
## Warning in .hist1(x, maxpixels = maxpixels, main = main, plot = plot, ...):
## 32% of the raster cells were used. 100000 values used.

chm[chm==0]<- NA
hist(chm,
     xlab = "Tree height (m)")

Import aspect data

aspect <- raster("../NEONdata/D17-California/TEAK/2013/lidar/TEAK_lidarAspect.tif")
plot(aspect)

Create Classification Matrix

#North facing versus south facing , 
#Create a matrix
class.m <- c(0, 45, 1,
             45, 135, NA,
             135, 225, 2,
             225, 315, NA,
             315,360, 1)
rcl.m<- matrix(class.m,
               ncol=3,
               byrow=T)
rcl.m
##      [,1] [,2] [,3]
## [1,]    0   45    1
## [2,]   45  135   NA
## [3,]  135  225    2
## [4,]  225  315   NA
## [5,]  315  360    1

reclassify raster

asp.ns <- reclassify(aspect,
                     rcl.m)
plot(asp.ns,
     main = "N and S Facing Slopes")

Export Geotiff

#take file, export to tiff file in outputs folder, 
writeRaster(asp.ns,
            file="../outputs/TEAK/Teak_nsAspect2.tif",
            options = "COMPRESS=LZW",
            NAflag = -9999)

Mask Data

asp.ns #did we have to add this here? 
## class       : RasterLayer 
## dimensions  : 577, 543, 313311  (nrow, ncol, ncell)
## resolution  : 1, 1  (x, y)
## extent      : 325963, 326506, 4102905, 4103482  (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=utm +zone=11 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
## data source : in memory
## names       : layer 
## values      : 0, 2  (min, max)
ndvi <- raster("../NEONdata/D17-California/TEAK/2013/spectrometer/veg_index/TEAK_NDVI.tif")

plot(ndvi,
     main = "NDVI for teakettle field site")

#mask data
nFacing.ndvi <- mask(ndvi,
                     asp.ns)
plot(nFacing.ndvi)